package com.lingxu.base.system.manage.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingxu.base.system.manage.entity.SysRole;
import com.lingxu.base.system.manage.entity.SysUser;
import com.lingxu.base.system.manage.entity.SysUserRole;
import com.lingxu.base.system.manage.mapper.SysRoleMapper;
import com.lingxu.base.system.manage.mapper.SysUserMapper;
import com.lingxu.base.system.manage.mapper.SysUserRoleMapper;
import com.lingxu.base.system.manage.service.ISysUserRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 用户角色关联表
 * @Author: LingXu
 * @Date:   2021-01-04
 * @Version: V1.0.1
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

     @Resource
     private SysUserRoleMapper sysUserRoleMapper;
     @Resource
     private SysUserMapper sysUserMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;


    /**
     * 用户角色关联表-分页列表查询
     * @param page 分页信息
     * @param sysUserRole 用户角色关联表
     * @return 用户角色关联表列表
     */
    @Override
    public IPage<SysUserRole> queryPageList(Page<SysUserRole> page, SysUserRole sysUserRole){
        return sysUserRoleMapper.queryList(page,sysUserRole);
    }

    /**
     * 用户角色关联表-列表查询
     * @param sysUserRole 用户角色关联表
     * @return 用户角色关联表列表
     */
    @Override
    public List<SysUserRole> queryList(SysUserRole sysUserRole){
        return sysUserRoleMapper.queryList(sysUserRole);
    }

    @Override
    public List<String> getRoleHavingUserList(String roleId) {
        return sysUserRoleMapper.getRoleHavingUserList(roleId);
    }

    @Override
    public void distributeRoleUsersAll(String roleId) {
        sysUserRoleMapper.deleteByRoleId(roleId);
        SysUser sysUser = new SysUser();
        List<SysUser> users = sysUserMapper.queryList(sysUser);
        List<SysUserRole> sysUserRoles=new ArrayList<>();
        for(SysUser user:users){
            SysUserRole sysUserRole=new SysUserRole();
            sysUserRole.setRoleid(roleId);
            sysUserRole.setUserid(user.getId());
            sysUserRoles.add(sysUserRole);
        }
        this.saveBatch(sysUserRoles);
    }

    @Override
    public void distributeRoleUsers(String roleId, List<String> userIds) {
        List<SysUserRole> sysUserRoles=new ArrayList<>();
        for(String userId:userIds){
            SysUserRole sysUserRole=new SysUserRole();
            sysUserRole.setRoleid(roleId);
            sysUserRole.setUserid(userId);
            sysUserRoles.add(sysUserRole);
        }
        this.saveBatch(sysUserRoles);
    }

    @Override
    public Integer cancelRoleUsers(String roleId, String userIds) {
        return sysUserRoleMapper.cancelRoleUsers(roleId,userIds);
    }

    @Override
    public Integer cancelRoleUsersAll(String roleId) {
        return sysUserRoleMapper.cancelRoleUsersAll(roleId);
    }


    @Override
    public Integer cancelUserRoles(String userId, String roleIds) {
       return sysUserRoleMapper.cancelUserRoles(userId,roleIds);
    }

    @Override
    public void distributeUserRolesAll(String userId) {
        //先取消该用户所有角色
        sysUserRoleMapper.cancelUserRolesAll(userId);
        List<SysRole> roles = sysRoleMapper.queryList();
        List<SysUserRole> sysUserRoles=new ArrayList<>();
        for(SysRole role:roles){
            SysUserRole sysUserRole=new SysUserRole();
            sysUserRole.setUserid(userId);
            sysUserRole.setRoleid(role.getId());
            sysUserRoles.add(sysUserRole);
        }
        this.saveBatch(sysUserRoles);
    }

    @Override
    public Integer cancelUserRolesAll(String userId) {
       return sysUserRoleMapper.cancelUserRolesAll(userId);
    }


    @Override
    public void distributeUserRoles(String userId,  List<String> roleIds) {
        List<SysUserRole> sysUserRoles=new ArrayList<>();
        for(String roleId:roleIds){
            SysUserRole sysUserRole=new SysUserRole();
            sysUserRole.setUserid(userId);
            sysUserRole.setRoleid(roleId);
            sysUserRoles.add(sysUserRole);
        }
        this.saveBatch(sysUserRoles);
    }

    @Override
    public List<SysRole> queryListByUserId(String userid) {
        return sysRoleMapper.queryListByUserId(userid);
    }
    @Override
    public List<String> getSysRoleIDExistByUserid(String userid) {
        return sysRoleMapper.getSysRoleIDExistByUserid(userid);
    }

    @Override
    public List<SysRole> getSysRoleExistByUserid(String userid) {
        return sysRoleMapper.queryListByUserId(userid);
    }

    @Override
    public List<SysRole> getSysRoleNotExistByUserid(String userid) {
        return sysRoleMapper.getSysRoleNotExistByUserid(userid);
    }
}
